<template>
  <comcard>
    <div slot="title_name">商品添加</div>
    <div slot="body_name">
      <el-form :model="searchObj">
        <el-form-item label="商品名称">
          <el-row>
            <el-col :span="6">
              <el-input
                type="text"
                placeholder="商品名称"
                v-model="searchObj.name"
              ></el-input
            ></el-col>
          </el-row>
        </el-form-item>

        <el-form-item label="商品分类">
          <el-select placeholder="请选择商品分类" v-model="searchObj.category">
            <el-option
              v-for="(item, index) in dataScous"
              :key="index"
              :value="item.cateName"
              :label="item.cateName"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品价格">
          <el-input-number
            v-model="searchObj.price"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
        </el-form-item>

        <el-upload
          class="avatar-uploader"
          action="http://127.0.0.1:5000/goods/goods_img_upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>

        <el-form-item label="商品描述">
          <el-row>
            <el-col :span="6">
              <div style="margin: 20px 0"></div>
              <el-input
                type="textarea"
                placeholder="请输入内容"
                v-model="searchObj.goodsDesc"
                maxlength="30"
                show-word-limit
              >
              </el-input>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="add()">添加商品</el-button>
        </el-form-item>
      </el-form>
    </div>
  </comcard>
</template>

<script>
import comcard from "../../components/comcard/index.vue";
import { api_listly } from "../../api/goods.js";
import { api_add } from "../../api/users.js";
export default {
  components: {
    comcard,
  },
  data() {
    return {
      imageUrl: "",
      dataScous: [],
      formObj: {
        currentPage: 1,
        pageSize: 3,
        name: "",
        category: "",
      },

      searchObj: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    //数据渲染
    async query() {
      let rst = await api_listly(this.formObj);
      console.log(rst);
      this.dataScous = rst.categories;
      // console.log(this.dataScous);
    },
    async add() {
      let rst = await api_add(this.searchObj);
      console.log(rst);
      let { code, msg } = rst;
      if (code == 0) {
        this.$router.push("/goods/list");
      } else {
        this.$message.error(msg);
      }
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = `http://localhost:5000/upload/imgs/goods_img/${res.imgUrl}`;
      this.searchObj.imgUrl = res.imgUrl;
      console.log(res, file);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>